// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/common/base/exceptions.h>
#include <pollux/testing/gtest_utils.h>
#include <pollux/common/testutil/scoped_test_time.h>
#include <pollux/common/time/timer.h>

namespace {

using namespace kumo::pollux;
using namespace kumo::pollux::common::testutil;

// NOTE: we can only construct ScopedTestTime in debug builds.
DEBUG_ONLY_TEST(TestScopedTestTime, testSetCurrentTimeMs) {
  {
    ScopedTestTime scopedTestTime;
    scopedTestTime.setCurrentTestTimeMs(1);
    ASSERT_EQ(getCurrentTimeMs(), 1);
    ASSERT_EQ(getCurrentTimeMicro(), 1000);
    scopedTestTime.setCurrentTestTimeMs(2);
    ASSERT_EQ(getCurrentTimeMs(), 2);
    ASSERT_EQ(getCurrentTimeMicro(), 2000);
  }

  // This should be the actual time, so we don't know what it is, but it
  // shouldn't be equal to the overridden value.
  ASSERT_NE(getCurrentTimeMs(), 2);
  ASSERT_NE(getCurrentTimeMicro(), 2000);
}

DEBUG_ONLY_TEST(TestScopedTestTime, testSetCurrentTimeMicro) {
  {
    ScopedTestTime scopedTestTime;
    scopedTestTime.setCurrentTestTimeMicro(1000);
    ASSERT_EQ(getCurrentTimeMs(), 1);
    ASSERT_EQ(getCurrentTimeMicro(), 1000);
    scopedTestTime.setCurrentTestTimeMicro(2000);
    ASSERT_EQ(getCurrentTimeMs(), 2);
    ASSERT_EQ(getCurrentTimeMicro(), 2000);
  }

  // This should be the actual time, so we don't know what it is, but it
  // shouldn't be equal to the overridden value.
  ASSERT_NE(getCurrentTimeMs(), 2);
  ASSERT_NE(getCurrentTimeMicro(), 2000);
}

DEBUG_ONLY_TEST(TestScopedTestTime, testSetCurrentTimeNano) {
  {
    ScopedTestTime scopedTestTime;
    scopedTestTime.setCurrentTestTimeNano(1000);
    ASSERT_EQ(getCurrentTimeMicro(), 1);
    ASSERT_EQ(getCurrentTimeNano(), 1000);
    scopedTestTime.setCurrentTestTimeNano(2000);
    ASSERT_EQ(getCurrentTimeMicro(), 2);
    ASSERT_EQ(getCurrentTimeNano(), 2000);
  }

  // This should be the actual time, so we don't know what it is, but it
  // shouldn't be equal to the overridden value.
  ASSERT_NE(getCurrentTimeMicro(), 2);
  ASSERT_NE(getCurrentTimeNano(), 2000);
}

DEBUG_ONLY_TEST(TestScopedTestTime, multipleScopedTestTimes) {
  {
    ScopedTestTime scopedTestTime;
    scopedTestTime.setCurrentTestTimeMs(1);
    ASSERT_EQ(getCurrentTimeMs(), 1);
    ASSERT_EQ(getCurrentTimeMicro(), 1000);
  }

  {
    ScopedTestTime scopedTestTime;
    // The previous scoped test time should have been cleared.
    ASSERT_NE(getCurrentTimeMs(), 1);
    ASSERT_NE(getCurrentTimeMicro(), 1000);

    scopedTestTime.setCurrentTestTimeMs(1);
    ASSERT_EQ(getCurrentTimeMs(), 1);
    ASSERT_EQ(getCurrentTimeMicro(), 1000);

    // Trying to create another ScopedTestTime with one already in scope should
    // fail.
    auto createScopedTestTime = []() { ScopedTestTime scopedTestTime2; };
    POLLUX_ASSERT_THROW(
        createScopedTestTime(),
        "Only one ScopedTestTime can be active at a time");
  }
}
} // namespace
